<template>

  <div class="app-container">

    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="最终发布"/>
    </el-steps>

    <el-button type="text" @click="openChapterDialog">添加章节</el-button>

    <!--遍历课程章节小节信息-->
    <!-- 章节 -->
    <ul class="chanpterList">
      <li
        v-for="chapter in chapterList"
        :key="chapter.id">
        <p>
          {{ chapter.title }}

          <span class="acts">
                    <el-button style="" type="text" @click="openVideo(chapter.id)">添加小节</el-button>
                    <el-button style="" type="text" @click="openEditChapter(chapter.id)">编辑</el-button>
                    <el-button type="text" @click="removeChapter(chapter.id)">删除</el-button>
                </span>
        </p>

        <!-- 视频 -->
        <ul class="chanpterList videoList">
          <li
            v-for="video in chapter.videoList"
            :key="video.id">
            <p>{{ video.title }}

              <span class="acts">

                    <el-button style="" type="text" @click="editVideo(video.id)">编辑</el-button>
                    <el-button type="text" @click="removeVideo(video.id)">删除</el-button>
                </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
    </div>


    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加和修改课时表单 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="video.free">
            <el-radio :label="true" :value="0">免费</el-radio>
            <el-radio :label="false" :value="1">默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上传视频">
          <el-upload
            :on-success="handleVodUploadSuccess"
            :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :action="BASE_API+'/vod/upload'"
            :limit="1"
            class="upload-demo">
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
              <div slot="content">最大支持1G，<br>
                支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                SWF、TS、VOB、WMV、WEBM 等视频格式上传</div>
              <i class="el-icon-question"/>
            </el-tooltip>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import chapter from '@/api/chapter/list'
  import video from '@/api/video/list'
  export default {
      name: "chapter",
      data(){
        return{
          BASE_API:process.env.BASE_API,//获取dev.env.js的BASE_API的值
          saveBtnDisabled:false,
          courseId: '',
          chapterList:[],
          dialogChapterFormVisible:false,//章节弹框
          dialogVideoFormVisible:false, //小节弹框
          saveVideoBtnDisabled:false,
          chapter:{ //封装章节数据
            courseId:'',
            title: '',
            sort: 0
          },
          video: {
            title: '',
            sort: 0,
            free: 0,
            videoSourceId: '',
            videoOriginalName: ''//上传后的视频名字
          },
          fileList: [] //文件列表
        }
      },
      methods:{
        //返回上一步
        previous(){
          this.$router.push({path:"/course/info/"+this.courseId})
        },
        //下一步
        next(){
          this.$router.push({path:"/course/publish/"+this.courseId})
        },
        //====================章节操作======================
        findAllByCourseId(id){
          chapter.findAllByCourseId(id).then(res=>{
            this.chapterList = res.data.chapters
          })
        },
        //打开添加章节对话框
        openChapterDialog(){
          this.dialogChapterFormVisible = true
        },
        //添加章节
        addChapter(){
          //为courseId赋值
          this.chapter.courseId = this.courseId
          //添加章节
          chapter.addChapter(this.chapter).then(res=>{
            //关闭弹框
            this.dialogChapterFormVisible = false
            //清空弹框中表单信息
            this.chapter = {}
            //提示信息
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            //重新加载章节信息
            this.findAllByCourseId(this.courseId)
          })
        },
        //修改章节
        updateChapter(){
          chapter.update(this.chapter).then(res=>{
            //关闭弹框
            this.dialogChapterFormVisible = false
            //清空弹框中表单信息
            this.chapter = {}
            //提示信息
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            //重新加载章节信息
            this.findAllByCourseId(this.courseId)
          })
        },
        //修改章节时数据回显
        openEditChapter(id){
          chapter.findById(id).then(res=>{
            this.chapter = res.data.chapter
          })
          //打开弹窗
          this.dialogChapterFormVisible = true
        },
        //删除章节
        removeChapter(id){
          //弹出确认框
          this.$confirm('此操作将永久删除该章节, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //确定之后删除该章节
            chapter.deleteById(id).then(res=>{
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              //刷新章节信息
              this.findAllByCourseId(this.courseId)
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        },
        //添加或修改章节
        saveOrUpdate(){
          if(!this.chapter.id){
            //添加章节
            this.addChapter()
          }else {
            //修改章节
            this.updateChapter()
          }
        },
        //====================小节操作======================
        openVideo(chapterId){
          //打开添加小节对话框
          this.dialogVideoFormVisible = true
          //添加id
          this.video.chapterId = chapterId
          this.video.courseId = this.courseId
        },
        //删除小节
        removeVideo(id){
          //弹出确认框
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //删除
            video.deleteById(id).then(res=>{
              //提示
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              //刷新章节信息
              this.findAllByCourseId(this.courseId)
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
        },
        //添加小节
        addVideo(){
          video.add(this.video).then(res=>{
            //清空添加小节表单的信息
            this.video = {}
            //提示信息
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            //关闭弹窗
            this.dialogVideoFormVisible = false
            //刷新章节信息
            this.findAllByCourseId(this.courseId)
          })
        },
        //更新小节
        updateVideo(videoVo){
          video.update(videoVo).then(res=>{
            //提示信息
            this.$message({
              type: 'success',
              message: '更新成功!'
            })
            //关闭弹窗
            this.dialogVideoFormVisible = false
            //刷新章节信息
            this.findAllByCourseId(this.courseId)
          })
        },
        //添加或更新小节
        saveOrUpdateVideo(){
          if(!this.video.id){ //添加小节
            this.addVideo()
          }else { //更新小节
            this.updateVideo(this.video)
          }
        },
        //编辑小节信息时回显信息
        showVideo(id){
          video.findById(id).then(res=>{
            this.video = res.data.video
          })
        },
        //编辑小节
        editVideo(id){
          //回显小节信息
          this.showVideo(id)
          //打开编辑小节对话框
          this.dialogVideoFormVisible = true
        }
        ,
        //===================视频操作===================
        //视频上传完成后
        handleVodUploadSuccess(res,file,fileList){
          this.video.videoSourceId = res.data.videoId
          this.video.videoOriginalName = file.name
          this.fileList = fileList
        },
        //删除视频之前,给与弹框提示,确定之后调用handleVodRemove方法来删除视频
        beforeVodRemove(file, fileList){
          return this.$confirm(`确定移除 ${ file.name }？`)
        },
        //删除视频
        handleVodRemove(file, fileList){
          //删除文件
          video.deleteVideo(this.video.videoSourceId).then(res=>{
          })
          //清空当前videoId
          this.video.videoSourceId = ''
          this.video.videoOriginalName = ''
          //清空文件列表
          this.fileList = []
        },
        handleExceed(){

        }
      },

      created() {
        //获取路径中的courseId
        if(this.$route.params && this.$route.params.id){
          this.courseId = this.$route.params.id
          this.findAllByCourseId(this.courseId)
        }
      }
    }
</script>

<style scoped>
  .chanpterList{
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
  }
  .chanpterList li{
    position: relative;
  }
  .chanpterList p{
    float: left;
    font-size: 20px;
    margin: 10px 0;
    padding: 10px;
    height: 70px;
    line-height: 50px;
    width: 100%;
    border: 1px solid #DDD;
  }
  .chanpterList .acts {
    float: right;
    font-size: 14px;
  }

  .videoList{
    padding-left: 50px;
  }
  .videoList p{
    float: left;
    font-size: 14px;
    margin: 10px 0;
    padding: 10px;
    height: 50px;
    line-height: 30px;
    width: 100%;
    border: 1px dotted #DDD;
  }
</style>
